package com.zls.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zls.pojo.SysUser;
import com.zls.pojo.TFarm;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface TFarmMapper extends BaseMapper<TFarm> {
    //分页查询
    //通过注解的方式实现映射
    //@Results 等价于 <ResultMap>标签
    //@Result  等价于<result>标签  column字段
    @Results(id = "tfarmMap", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "province", property = "province"),
            @Result(column = "city", property = "city"),
            @Result(column = "district", property = "district"),
            @Result(column = "type", property = "type"),
            @Result(column = "name", property = "name"),
            @Result(column = "linkman", property = "linkman"),
            @Result(column = "telephone", property = "telephone"),
            @Result(column = "remark", property = "remark"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "main_crop", property = "mainCrop"),
            @Result(column = "area", property = "area"),
            @Result(column = "img", property = "img"),
            @Result(column = "user_id", property = "sysUser", javaType = SysUser.class, one =
                    //FetchType.LAZY 懒加载   EAGER:立即加载
                    //select 代表的关联查询  调用指定的mapper方法
            @One(select = "com.zls.mapper.SysUserMapper.selectById", fetchType = FetchType.EAGER))
    })
    @Select("<script>" +
            "select * from t_farm where 1=1" +
            "<if test='queryString!=null'> and name like concat( '%',#{queryString},'%')" +
            "limit #{currentPage},#{pageSize}</if>" +
            "<if test='queryString==null'>limit #{currentPage},#{pageSize}</if> "+
            "</script>")
    List<TFarm> findPage(@Param("currentPage") Integer currentPage, @Param("pageSize") Integer pageSize, @Param("queryString") String queryString);

    //查询总记录数
    @Select("<script>" +
            "select count(*) from t_farm where 1 =1" +
            "<if test='queryString!=null'> and name like concat( '%',#{queryString},'%') </if>" +
            "</script>")
    Long count(String queryString);

    //根据id查
    @ResultMap("tfarmMap")
    @Select("select * from t_farm where id=#{id}")
    TFarm selectId(int id);



}
